package com.longrise.android.byjk.plugins.hra.hratabfirst;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.Control;
import com.longrise.android.byjk.model.CourseDefaultItem;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/1/5.
 * Hra服务点---每天可预约的rcvadapter
 */

public class HraPlacesAdapter extends BaseMultiItemQuickAdapter<OrderPlaceDefaultItem, BaseViewHolder> {

    public static final String TAG = "HraPlacesAdapter";
    private OnDetailItemClickListener mListener;
    private List<OrderPlaceDefaultItem> mParentBeanList = new ArrayList<>();
    private String mDate;


    public HraPlacesAdapter(List<OrderPlaceDefaultItem> data) {
        super(data);
        addItemType(CourseDefaultItem.TYPE1,R.layout.hra_places_null_view);
        addItemType(CourseDefaultItem.TYPE2,R.layout.item_hra_places);
    }

    public void refreshData(List<OrderPlaceDefaultItem> orderDefaultItem) {
        mParentBeanList.clear();
        mParentBeanList.addAll(orderDefaultItem);
        setNewData(orderDefaultItem);
    }

    @Override
    protected void convert(BaseViewHolder helper, final OrderPlaceDefaultItem item) {
        if(HraOrderDefaultItem.TYPE1 == item.getItemType()){
            HraServiceEvent event = new HraServiceEvent();
            event.setSelected(false);
            EventBus.getDefault().post(event);
        }else if(HraOrderDefaultItem.TYPE2 == item.getItemType()){
            final OrderPlaceChildBean orderPlaceBean = item.getOrderPlaceBean();
            View convertView = helper.getConvertView();
            final int position = helper.getLayoutPosition();
            mDate = orderPlaceBean.getBespeakdate();
            final String childId = mDate + position;
            item.setId(childId);
            final LinearLayout ll_item_hra_places = (LinearLayout) convertView.findViewById(R.id.ll_item_hra_places);
            TextView item_hra_time = (TextView) convertView.findViewById(R.id.tv_item_hra_time);
            TextView item_hra_counts = (TextView) convertView.findViewById(R.id.tv_item_hra_counts);
            item_hra_time.setText(orderPlaceBean.getTimeslot());
            item_hra_counts.setText("可预约名额: " + orderPlaceBean.getStockpeople()+"人");

            if (childId.equals(Control.getBookDataId())){
                String timeslot = orderPlaceBean.getTimeslot();
                String timeslotid = orderPlaceBean.getTimeslotid();
                String machineid = orderPlaceBean.getMachineid();
                String price = orderPlaceBean.getPrice();
                String machineType = orderPlaceBean.getMachineType();
                String machinemodel = orderPlaceBean.getMachinemodel();
                HraServiceEvent event = new HraServiceEvent();
                event.setSelected(true);
                event.setTimeslot(timeslot);
                event.setTimeslotid(timeslotid);
                event.setMachineId(machineid);
                event.setBespeakdate(mDate);
                event.setPrice(price);
                event.setMachineType(machineType);
                event.setMachinModel(machinemodel);
                EventBus.getDefault().post(event);
                ll_item_hra_places.setBackgroundResource(R.drawable.bg_hra_place_select_shape);
            }else {
                ll_item_hra_places.setBackgroundResource(R.drawable.bg_hra_place_unselect_shape);
            }
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onClick(orderPlaceBean,position);
                        Control.setBookDataId(childId);
                        ll_item_hra_places.setBackgroundResource(R.drawable.bg_hra_place_select_shape);
                        notifyDataSetChanged();
                    }
                }
            });


            if(mParentBeanList.size() != 0){
                OrderPlaceChildBean orderPlaceBean1 = mParentBeanList.get(0).getOrderPlaceBean();
                String price = orderPlaceBean1.getPrice();
                String machineType = orderPlaceBean1.getMachineType();
                HraPriceEvent event = new HraPriceEvent();
                event.setPrice(price);
                event.setMachineType(machineType);
                EventBus.getDefault().postSticky(event);
            }
        }
    }

    public void setOnHraServiceItemClickListener(OnDetailItemClickListener listener) {
        this.mListener = listener;
    }


    public interface OnDetailItemClickListener {
        void onClick(OrderPlaceChildBean item,int position);
    }
}
